<div id="classtree_top" class="classtree-topbar">
	<a class="newdir" href="javascript:;" onclick="jstree_create_organization();return false;" title="{lang creation_agency}"></a>
	<a class="newdir_1" href="javascript:;" onclick="jstree_create_dir();return false;" title="{lang creation_bottom_section}"></a>
	<a class="newdoc" href="javascript:;" onclick="jstree_create_user();return false;" title="{lang add_user}"></a>
	<!--{if $_G['adminid']==1}-->
	<a class="import " href="{ADMINSCRIPT}?mod=orguser&op=import" title="{lang import_user}"></a>
	<!--{/if}-->
	
	<a class="guide" href="javascript:;" onclick="show_guide();return false;" title="{lang moderate_explain}"></a>
</div>
<div class="classtree-search">
	<a href="javascript:;" class="search" onclick="jstree_search(jQuery('#jstree_search_input').val());return false" title="{lang search}"><i class="glyphicon glyphicon-search"></i></a>
	<a href="javascript:;" class="delete" onclick="jstree_search('stop');return false" title="{lang close_search}"><i class="glyphicon glyphicon-remove"></i></a>
	<input id="jstree_search_input" type="text" class="form-control" placeholder="{lang enter_search}" onkeyup="if(event.keyCode==13){jstree_search(jQuery('#jstree_search_input').val())}" />
</div>
<div id="classtree" class="classtree-container" style="padding:5px 0;overflow:auto;border-top:1px solid #FFF;"></div>

<script type="text/javascript">
	var orgtree = $orgtree;
	var ajaxurl = '{ADMINSCRIPT}?mod=orguser&op=ajax&';
	var baseurl = '{ADMINSCRIPT}?mod=orguser&';
	jQuery(document).ready(function(e) {
		jQuery("#classtree").jstree({
			"core": {
				"multiple": true,
				'check_callback': function(operation, node, node_parent, node_position, more) {
					// operation can be 'create_node', 'rename_node', 'delete_node', 'move_node' or 'copy_node'
					// in case of 'rename_node' node_position is filled with the new node name
					if(node.id == 'other') return false;
					if(node_parent.id == 'other' && node.type != 'user') return false;
					return operation === 'copy_node' && node.type == 'organization' ? false : true;
				},
				"themes": { "responsive": false, "variant": 'large' },
				"data": {
					dataType: 'json',
					url: '{ADMINSCRIPT}?mod=orguser&op=ajax&do=getchildren&t=' + new Date().getTime(),
					data: function(node) {
						return { 'id': node.id };
					}
				}
			},
			"dnd": {
				"copy": true,
				"open_timeout": 500,
				"is_draggable": function(node) {
					var inst = jQuery("#classtree").jstree(true);
					for(var i in node) {
						if(inst.is_disabled(node[i])) return false;
					}
					return true;
				}
			},

			"types": {
				"#": {
					"max_children": -1,
					"max_depth": -1,
					"valid_children": -1
				},
				"organization": {
					"valid_children": ['user', 'organization']
				},
				"group": {
					"icon":"dzz dzz-group",
					"valid_children": ['user']
				},
				"default": {
					"valid_children": ['user']
				},
				"disabled": {
					//  "icon" : "jstree-icon-file",
					"valid_children": []
				},
				"user": {
					//  "icon" : "jstree-icon-file",
					"valid_children": []
				}
			},
			'contextmenu': {
				'select_node': false,
				'show_at_node': false,
				'items': {
					"create_sibing": {
						"separator_before": false,
						"separator_after": false,
						"_disabled": function(data) {
							var inst = jQuery.jstree.reference(data.reference);
							var node = inst.get_node(data.reference);
							if(node.type == 'default') return true;
							//if(node.type == 'user') return true;
							if(inst.is_disabled(node)) {
								return true;
							}
							var parent = inst.get_node(node.parent);
							if(inst.is_disabled(parent)) {
								return true;
							}
							return false;
						},
						"label": "{lang creation_equally_section}",
						"icon": "glyphicon glyphicon-tag",
						"action": function(data) {
							var inst = jQuery.jstree.reference(data.reference),
								obj = inst.get_node(data.reference),
								obj1 = inst.get_node(obj.parent);

							var position = jQuery.inArray(obj.id, obj1.children) + 1;
							jQuery.post('{ADMINSCRIPT}?mod=orguser&op=ajax&do=create', { 'forgid': obj.parent, 'orgid': obj.id, 't': new Date().getTime() }, function(json) {

								if(!json || json.error) {
									showmessage(json.error, 'danger', 3000, 1);
								} else if(json.orgid > 0) {
									var arr = { "id": json.orgid, "text": json.orgname, "type": "organization", "icon": (json.forgid > 0) ? 'dzz/system/images/department.png' : 'dzz/system/images/organization.png' }
									inst.create_node(obj1, arr, position, function(new_node) {
										setTimeout(function() { inst.edit(new_node); }, 0);
									});
								}
							}, 'json');
						}
					},

					"create": {
						"separator_before": false,
						"separator_after": false,
						"_disabled": function(data) {
							var inst = jQuery.jstree.reference(data.reference);
							var node = inst.get_node(data.reference);
							var parent = inst.get_node(node.parent);
							if(node.type == 'group') return true;
							if(node.type == 'default') return true;
							if(node.type == 'user') return true;
							if(inst.is_disabled(node)) {
								return true;
							}
							return false;
						},
						"label": "{lang creation_bottom_section}",
						"icon": "glyphicon glyphicon-tags",
						"action": function(data) {
							var inst = jQuery.jstree.reference(data.reference),
								obj = inst.get_node(data.reference);
							inst.open_node(obj,function(){
								jQuery.post('{ADMINSCRIPT}?mod=orguser&op=ajax&do=create', { 'forgid': obj.id, 't': new Date().getTime() }, function(json) {
									if(!json || json.error) {
										showmessage(json.error, 'danger', 3000, 1);
									} else if(json.orgid > 0) {
										var arr = { "id": json.orgid, "text": json.orgname, "type": "organization", "icon": (json.forgid > 0) ? 'dzz/system/images/department.png' : 'dzz/system/images/organization.png' };
	
										inst.create_node(obj, arr, "first", function(new_node) {
											setTimeout(function() { inst.edit(new_node); }, 0);
										});
									}
								}, 'json');
							});
							
						}
					},

					"rename": {
						"separator_before": false,
						"separator_after": false,
						"_disabled": function(data) {
							var inst = jQuery.jstree.reference(data.reference);
							var node = inst.get_node(data.reference);
							if(node.type == 'default') return true;
							if(inst.is_disabled(node)) {
								return true;
							}
							if(node.type == 'user') return true;

							return false;
						},
						"label": "{lang rechristen}",
						
						"shortcut"			: 113,
						"shortcut_label"	: 'F2',
						"icon": "glyphicon glyphicon-leaf",

						"action": function(data) {

							var inst = jQuery.jstree.reference(data.reference),
								obj = inst.get_node(data.reference);
							console.log('aaaaaaa');
							inst.edit(obj);

						}
					},
					"remove": {
						"separator_before": false,
						"icon": false,
						"separator_after": false,
						"_disabled": function(data) {
							var inst = jQuery.jstree.reference(data.reference);
							var node = inst.get_node(data.reference);
							if(node.type == 'default') return true;
							if(inst.is_disabled(node)) {
								return true;
							}
							var parent = inst.get_node(node.parent);
							if(inst.is_disabled(parent)) {
								return true;
							}

							return false;
						},
						"label": "{lang delete}",
						"icon": "glyphicon glyphicon-remove",
						"action": function(data) {
							var inst = jQuery.jstree.reference(data.reference),
								obj = inst.get_node(data.reference);

							//判断是否为相同类型的多选和相同部门的
							var msg = '';
							var nodes = [];
							if(inst.is_selected(obj)) {
								var nodes = inst.get_selected(true);
								var type = null;
								var parent = null;
								for(var i in nodes) {
									if(!type) type = nodes[i].type;
									else if(type != nodes[i].type) {
										msg = '{lang please_select_same_type_node}';
										break;
									}
									if(!parent) parent = nodes[i].parent;
									else if(parent != nodes[i].parent) {
										msg = '{lang please_select_same_section_node}';
										break;
									}
								}
							} else {
								var nodes = [obj];
							}
							if(msg) {
								showmessage(msg, 'danger', 3000, 1);
								return;
							}
							if(obj.parent == 'other') {
								var uids = [];
								for(var i in nodes) {
									uids.push(nodes[i].li_attr.uid);
								}
								var msg = '{lang orguser_tree_delete}';

								var data = { 'uids': uids, 'forgid': obj.parent, 'type': 'user', 'realdelete': '1' };
							} else {
								if(obj.type == 'user') {
									var uids = [];
									for(var i in nodes) {
										uids.push(nodes[i].li_attr.uid);
									}

									var msg = '{lang orguser_tree_permission_delete}';
									var data = { 'uids': uids, 'forgid': obj.parent, 'type': 'user' };

								} else {
									if(nodes.length > 1) {
										showmessage('{lang orguser_tree_batch_delete}', 'danger', 3000, 1);
										return;
									}
									var msg = '{lang orguser_tree_all_delete}';
									var data = { 'orgid': obj.id, 'forgid': obj.parent }
								}
							}
							Confirm(msg,function(){
								jQuery.post('{ADMINSCRIPT}?mod=orguser&op=ajax&do=delete&t=' + new Date().getTime(), data, function(json) {
									if(json.error) {
										showmessage(json.error, 'danger', 3000, 1);
									} else {
										if(inst.is_selected(obj)) {
											inst.delete_node(inst.get_selected(true));
										} else {
											inst.delete_node(obj);
										}
										if(obj.type == 'user') {
											var parent = inst.get_node('other');
											inst.refresh_node(parent);
										}
									}

								}, 'json');
								});
							
						}
					}
				}
			},
			"search": {
				"show_only_matches": true,
				"fuzzy": false,
				"ajax": { 'url': '{ADMINSCRIPT}?mod=orguser&op=ajax&do=search', 'dataType': 'json' }
			},
			"plugins": ["unique", "contextmenu", "dnd", "types", "search",'wholerow']
			// List of active plugins

		});

		jQuery("#classtree").on('ready.jstree', function(e) {
			var inst = jQuery("#classtree").jstree(true);
			for(var i in orgtree) {
				if(document.getElementById(orgtree[i][0])) open_node_dg(inst, document.getElementById(orgtree[i][0]), orgtree[i]);
			}
		});
		jQuery(document).on('touchend', '#classtree .jstree-anchor', function() {
			var inst = jQuery("#classtree").jstree(true);
			var node = jQuery(this).closest('.jstree-node');
			inst.select_node(node);
			return false;
		});
		jQuery("#classtree").on('select_node.jstree', function(e, data) {
			var inst = jQuery("#classtree").jstree(true);
			inst.open_node(data.node);
			if(data.selected.length > 1) return;
			if(data.node.id == "other") return;
			if(data.node.type == 'user') {
				showDetail(data.node.li_attr.uid, 'user');
			} else {
				showDetail(data.node.id, 'organization');
			}
		});

		jQuery("#classtree").on('load_node.jstree', function(e, data) { //设置空节点为leaf
			var inst = jQuery("#classtree").jstree(true);
			if(data.node.children.length < 1) {
				jQuery('#' + data.node.id).removeClass('jstree-closed').addClass('jstree-leaf');;
			}
		});

		jQuery(document).on('dnd_move.vakata', function(e, data) {
			if(jQuery(data.event.target).closest('.moderators-acceptor').length) {
				jQuery('.moderators-acceptor').addClass('hover').find('img').attr('src', 'avatar.php?uid=' + jQuery(data.element).parent().attr('uid') + '&size=middle');
			} else {
				jQuery('.moderators-acceptor').removeClass('hover');
			}
		});
		jQuery(document).on('dnd_stop.vakata', function(e, data) {
			if(!jQuery(data.event.target).closest('.moderators-acceptor').length) return;
			var uid = jQuery(data.element).parent().attr('uid');
			var orgid = jQuery('.moderators-acceptor').attr('orgid');
			moderator_add(orgid, uid);
		});
		jQuery("#classtree").on('move_node.jstree', function(e, data) {
			if(jQuery(e.target).closest('.moderators-acceptor').length) return;
			var inst = jQuery("#classtree").jstree(true);
			var node = data.node;
			var parent = inst.get_node(data.parent);
			var post = {};
			var needsave = 0;
			if(node.type == 'user' && data.parent != data.old_parent) { //移动用户
				post.uid = node.li_attr.uid;
				post.forgid = data.old_parent;
				post.orgid = data.parent;
				post.type = 'user';
				needsave = 1;
			} else {
				post.orgid = node.id;
				post.forgid = data.parent;
				post.position = data.position;
				needsave = 1;
			}
			if(needsave) {
				jQuery.post('{ADMINSCRIPT}?mod=orguser&op=ajax&do=move&t=' + new Date().getTime(), post, function(json) {
					if(json.error) {
						showmessage(json.error, 'danger', 3000, 1, null, function() {
							window.location.reload();
						});
					}
				}, 'json');
			}
		});
		jQuery("#classtree").on('copy_node.jstree', function(e, data) {
			if(jQuery(e.target).closest('.moderators-acceptor').length) return;
			var inst = jQuery("#classtree").jstree(true);
			var node = data.node;
			var parent = inst.get_node(data.parent);
			var post = {};
			var needsave = 0;
			if(node.type == 'user' && data.parent != data.old_parent) { //移动用户
				post.uid = node.li_attr.uid;
				post.forgid = data.old_parent;
				post.orgid = data.parent;
				post.type = 'user';
				post.copy = 1;
				needsave = 1;
			} else {
				return;
			}
			if(needsave) {
				jQuery.post('{ADMINSCRIPT}?mod=orguser&op=ajax&do=move&t=' + new Date().getTime(), post, function(json) {

				}, 'json');
			}
		});
		jQuery("#classtree").on('open_node.jstree', function(e, data) {
			//jQuery("#"+data.node.id+" .jstree-anchor").addTouch();
		});
		jQuery("#classtree").on('rename_node.jstree', function(e, data) {
			var inst = jQuery("#classtree").jstree(true);
			var obj = inst.get_node(data.node);
            var oldtext = data.old.replace(/<(.+?)>(.+?)<\/(.+?)>/,'');
			if(data.text == '' || data.text == oldtext) {
				inst.set_text(obj, data.old);
				return;
			}
			jQuery.post('{ADMINSCRIPT}?mod=orguser&op=ajax&do=rename&t=' + new Date().getTime(), { 'orgid': data.node.id, text: data.text }, function(json) {
				if(!json || json.error) {
					obj.text = data.old;
					inst.set_text(obj, data.old);
					if(json.error) showmessage(json.error, 'danger', 3000, 1);
				}else{
					jQuery('#title_orgname').html(data.text);
					jQuery('#orgname').val(data.text).data('oldname',data.text);
					inst.refresh();
				}
			}, 'json');
		});
		// jQuery("#classtree").on('create_node.jstree',function(e,data){});

	});
</script>